package com.gm.wholesale.bluetooth.weightBlue;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

import com.gm.wholesale.bluetooth.BluetoothUtil;
import com.gm.wholesale.bluetooth.bean.WeightBean;
import com.gm.wholesale.bluetooth.interfaces.OnConnectionBluListener;
import com.gm.wholesale.bluetooth.service.BluetoothService;

public class BaseWeightBluetooth {

    public Handler mHandler;
    public Context mContext;
    public OnConnectionBluListener mListener;
    public static BluetoothUtil mBluetoothUtil;
    public BluetoothService mBluetoothService;


    public void stopFindDevice(Context context){
        if (mBluetoothUtil==null)return;
        mBluetoothUtil.cancelDiscovery(context);
    }

    public WeightBean mWeightBean = new WeightBean();

    public BaseWeightBluetooth setOnConnectionListener(OnConnectionBluListener listener) {
        mListener = listener;
        return this;
    }


    public String getConnectionDeviceAd(Context context){
        if (mBluetoothService!=null && mBluetoothService.mdevice!=null){
            return mBluetoothService.mdevice.getAddress();
        }
        return null;
    }

    public void write(byte  [] data){
        if (data!=null && mBluetoothService!=null){
            mBluetoothService.write(data);
        }
    }

    public void startConnectWeightDevice(Context context, String device) {
        if (!TextUtils.isEmpty(device) && mBluetoothService!=null) {
            mBluetoothUtil.connectDevice(context,device,mBluetoothService);
        }
    }



    public void setFailState() {
        if (mBluetoothService!=null){
            mBluetoothUtil.setFailState(mBluetoothService);
        }

    }
    public void close(){
        if (mBluetoothService!=null){
            mBluetoothService.stop();
        }
    }

    public void destory(){
        close();
        mHandler=null;
        mContext=null;
        mListener=null;
        mBluetoothUtil=null;
    }
}
